# import matplotlib.pyplot as plt
# from skimage import io
#
# file_name = '2.jpg'
# img=io.imread(file_name)
#
# img = img * 1.0
# img_out = img * 1.0
#
# # -1 ~ 1
#
# Increment = 0.5
#
# img_min = img.min(axis=2)
# img_max = img.max(axis=2)
#
# Delta = (img_max - img_min) / 255.0
# value = (img_max + img_min) / 255.0
# L = value/2.0
#
# mask_1 = L < 0.5
#
# s1 = Delta/(value + 0.001)
# s2 = Delta/(2 - value + 0.001)
# s = s1 * mask_1 + s2 * (1 - mask_1)
#
# if Increment >= 0 :
#     temp = Increment + s
#     mask_2 = temp >  1
#     alpha_1 = s
#     alpha_2 = s * 0 + 1 - Increment
#     alpha = alpha_1 * mask_2 + alpha_2 * (1 - mask_2)
#     alpha = 1/(alpha + 0.001) -1
#     img_out[:, :, 0] = img[:, :, 0] + (img[:, :, 0] - L * 255.0) * alpha
#     img_out[:, :, 1] = img[:, :, 1] + (img[:, :, 1] - L * 255.0) * alpha
#     img_out[:, :, 2] = img[:, :, 2] + (img[:, :, 2] - L * 255.0) * alpha
#
# else:
#     alpha = Increment
#     img_out[:, :, 0] = L * 255.0 + (img[:, :, 0] - L * 255.0) * (1 + alpha)
#     img_out[:, :, 1] = L * 255.0 + (img[:, :, 1] - L * 255.0) * (1 + alpha)
#     img_out[:, :, 2] = L * 255.0 + (img[:, :, 2] - L * 255.0) * (1 + alpha)
#
#
# img_out = img_out/255.0
#
# # 饱和处理
# mask_1 = img_out  < 0
# mask_2 = img_out  > 1
#
# img_out = img_out * (1-mask_1)
# img_out = img_out * (1-mask_2) + mask_2
#
# plt.figure()
# plt.imshow(img/255.0)
# plt.axis('off')
#
# plt.figure(2)
# plt.imshow(img_out)
# plt.savefig('22.jpg')
# plt.axis('off')
#
# plt.show()


import cv2
import numpy as np
from utils import LinearTran, turncolorBG, enchcolor

_DILATE_KERNEL = np.array([[0, 0, 1, 0, 0],
                           [0, 0, 1, 0, 0],
                           [1, 1, 1, 1, 1],
                           [0, 0, 1, 0, 0],
                           [0, 0, 1, 0, 0]], dtype=np.uint8)


def dilate(img):
    dilated = cv2.dilate(img, _DILATE_KERNEL)
    return dilated


enchcolor(file_name='15.jpg', save='CCLLtep.jpg')
img = cv2.imread('CCLLtep.jpg')
timg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, timg = cv2.threshold(timg, timg.mean(), 255, cv2.THRESH_BINARY)  # 做MASK与原图操作，背景变白，字体公章不变
b = np.zeros((img.shape[0], img.shape[1]), dtype=img.dtype)
g = np.zeros((img.shape[0], img.shape[1]), dtype=img.dtype)
r = np.zeros((img.shape[0], img.shape[1]), dtype=img.dtype)

b[:, :] = img[:, :, 0]  # 复制 b 通道的数据
g[:, :] = img[:, :, 1]  # 复制 g 通道的数据
r[:, :] = img[:, :, 2]  # 复制 r 通道的数据

tr = turncolorBG(r, timg)
tb = turncolorBG(b, timg)
tg = turncolorBG(g, timg)
pimg = np.dstack((tb, tg, tr))

cv2.imshow(';mask', timg)
cv2.imshow('pig', pimg)

img = cv2.cvtColor(pimg, cv2.COLOR_BGR2HSV)

lower_hsv = np.array([156, 43, 46])
upper_hsv = np.array([180, 255, 255])
mask1 = cv2.inRange(img, lower_hsv, upper_hsv)
lower_hsv = np.array([0, 43, 46])
upper_hsv = np.array([10, 255, 255])
mask2 = cv2.inRange(img, lower_hsv, upper_hsv)

mask3 = mask1 + mask2
mask3 = dilate(mask3)
_, mask3 = cv2.threshold(mask3, mask3.mean(), 255, cv2.THRESH_BINARY)
re1 = img[:, :, 0] * mask3
_, re1 = cv2.threshold(re1, re1.mean(), 255, cv2.THRESH_BINARY)
re1p = cv2.medianBlur(re1, 3)
cv2.imshow('re1', np.concatenate([re1, re1p], axis=1))
re2 = img[:, :, 1] * mask3
re2p = cv2.medianBlur(re2, 3)
re3 = img[:, :, 2] * mask3
cv2.imshow('re123', np.concatenate([re1, re2, re3], axis=1))
re3p = cv2.medianBlur(re3, 3)
re = np.dstack((re1, re2, re3))
rep = np.dstack((re1p, re2p, re3p))
cv2.imshow('mask3', mask3)
cv2.imshow('re', np.concatenate([re, rep], axis=1))

cv2.waitKey()
cv2.destroyAllWindows()
